package com.lancer.cloud.common.response;

import com.lancer.cloud.common.enums.ApiResponseCodeEnum;
import java.io.Serializable;
import lombok.Data;

/**
 * 响应实体
 *
 * @author caijinbang
 * @date 2019-06-15 23:14
 */
@Data
public class ApiResponse<T>  implements Serializable {

    private static final long serialVersionUID = 6973098599396040883L;

    private T data;

    private Integer code = ApiResponseCodeEnum.SUCCESS.getCode();

    private String message = ApiResponseCodeEnum.SUCCESS.getMessage();

    public ApiResponse(){

    }

    /**
     * 只返回数据
     * @param data
     */
    public ApiResponse(T data) {
        this.data =data;
    }


    public ApiResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ApiResponse(T data, ApiResponseCodeEnum responseCode) {
        this.data = data;
        this.code = responseCode.getCode();
        this.message = responseCode.getMessage();
    }


    /**
     * 异常 + 错误编码
     * @param throwable
     */
    public ApiResponse(Throwable throwable) {
        this.message = throwable.getMessage();
        this.code = ApiResponseCodeEnum.FAIL.getCode();
    }

    /**
     * 异常 + 返回编码
     * @param throwable
     * @param code
     */
    public ApiResponse(Throwable throwable, ApiResponseCodeEnum  code) {
        this.message = throwable.getMessage();
        this.code = code.getCode();
    }
}
